package com.example.springbootdemo.validation;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

public class CharsetValidator implements ConstraintValidator<ValidCharset, String> {

    private static final List<String> VALID_CHARSETS = Arrays.asList(
            "UTF-8", "ISO-8859-1", "US-ASCII", "UTF-16", "UTF-16BE", "UTF-16LE"
    );

    @Override
    public boolean isValid(String charset, ConstraintValidatorContext context) {
        if (charset == null) {
            return true; // 允许为空，使用默认值
        }
        return VALID_CHARSETS.contains(charset) && Charset.isSupported(charset);
    }
}